package com.huaxiafinance.management.util;


import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import org.apache.log4j.Logger;


public class PinyinUtil
{
    private static final Logger log = Logger.getLogger(PinyinUtil.class);

    private static HanyuPinyinOutputFormat format = null;

    static
    {
        format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.UPPERCASE);

        // WITHOUT_TONE：无音标 (zhong)
        // WITH_TONE_NUMBER：1-4数字表示英标 (zhong4)
        // WITH_TONE_MARK：直接用音标符（必须WITH_U_UNICODE否则异常） (zhòng)
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        // WITH_V：用v表示ü (nv)
        // WITH_U_AND_COLON：用"u:"表示ü (nu:)
        // WITH_U_UNICODE：直接用ü (nü)
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
    }

    private PinyinUtil()
    {

    }

    public static String getFullSpell(String value)
    {
        String temp = "";
        StringBuffer stringBuffer = new StringBuffer();
        if (value == null || value.trim().isEmpty())
        {
            return temp;
        }
        value = value.trim();

        char[] tempArr = value.toCharArray();

        for (int i = 0; i < tempArr.length; i++ )
        {
            try
            {
                String[] pinyin = PinyinHelper.toHanyuPinyinStringArray(tempArr[i], format);
                stringBuffer.append(pinyin[0]);
            }
            catch (Exception e)
            {
                log.error("错误++++++" + e);
                stringBuffer.append(tempArr[i]);
            }
        }
        temp = stringBuffer.toString();
        return temp.toUpperCase();
    }

    public static String getFirstSpell2(String value)
    {
        String temp = "";
        StringBuffer stringBuffer = new StringBuffer();
        if (value == null || value.trim().isEmpty())
        {
            return temp;
        }
        value = value.trim();

        char[] tempArr = value.toCharArray();

        for (int i = 0; i < tempArr.length; i++ )
        {
            try
            {
                String[] pinyin = PinyinHelper.toHanyuPinyinStringArray(tempArr[i], format);
                stringBuffer.append(pinyin[0].toCharArray()[0]);
            }
            catch (Exception e)
            {
                log.error("错误++++++" + e);
                stringBuffer.append(tempArr[i]);
            }
        }
        temp = stringBuffer.toString();
        return temp.toUpperCase();
    }
}